package com.example.jn_dxs.Utlis;

import android.content.Context;
import android.os.SystemClock;


import com.example.jn_dxs.Base.Observer;
import com.example.jn_dxs.view.LoadingDialog;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoadingUtil {

    private static LoadingDialog dialog;

    public static void showLoading(Context context, String... msg) {
        dialog = new LoadingDialog(context);
        dialog.show();
    }

    public static void showSuccess(Context context,String... text) {
        dialog = new LoadingDialog(context);
        dialog.show();
        Observable.create((ObservableOnSubscribe<Integer>) emitter -> {
            SystemClock.sleep(800);
            emitter.onNext(1);
        }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Integer>() {
            @Override
            public void onNext(Integer integer) {
                super.onNext(integer);
                dialog.dismiss();
            }
        });
    }

    public static void hide() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public static void setStatus(Context context, int status) {
        if (status == 1) {
            showLoading(context);
        } else {
            hide();
        }
    }
}
